package cn.i7baozh.cloud.payment.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.i7baozh.cloud.api.dto.PayDTO;
import cn.i7baozh.cloud.api.dto.ReturnResponse;
import cn.i7baozh.cloud.payment.entities.Pay;
import cn.i7baozh.cloud.payment.service.PayService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author : zlmismeno1
 * @date :   2024/12/25 11:14
 */
@RestController
@Slf4j
public class PayController {

    @Resource
    private PayService payService;


    //这种情况只会在启动的时候获取一次最新值，后续远程更改了跟他没关系
    @Value("${test.info:}")
    private String remoteTestInfo;

    @Value("${server.port}")
    private Integer port;

    @GetMapping("/provider/pay/getServerInfo")
    public ReturnResponse<String> getServerInfo() {
        String msg = String.format("来自端口：%s的服务器响应", port);
        return ReturnResponse.success(msg);
    }

    /**
     * 这种情况每次能够及时拿到最新值
     *
     * @param remoteValue
     * @return
     */
    @GetMapping("/provider/pay/getRemoteInfo")
    public ReturnResponse<String> getRemoteInfo(@Value("${test.info:}") String remoteValue) {
        log.info("remoteValue: {}", remoteValue);
        return ReturnResponse.success(remoteTestInfo);
    }

    @GetMapping("/provider/pay/get/{id}")
    public ReturnResponse<PayDTO> get(@PathVariable(value = "id") Integer id) {
        //feign 重试机制
        log.info("访问了: {}", port );
        if (id < 0) {
            //模拟报错
            throw new RuntimeException("不存在啊，查什么查");
        }
        if (id == 100) {
            try{
                //模拟超时
                TimeUnit.SECONDS.sleep(62);
            } catch (Exception e) {
                log.error("Error,", e);
            }
        }
        return ReturnResponse.success(BeanUtil.copyProperties(payService.selectById(id), PayDTO.class));
    }

    @GetMapping("/provider/pay/getAll")
    public ReturnResponse<List<PayDTO>> get() {
        return ReturnResponse.success(BeanUtil.copyToList(payService.selectAll(), PayDTO.class));
    }

    @DeleteMapping("/provider/pay/delete/{id}")
    public ReturnResponse<String> delete(@PathVariable(value = "id") Integer id) {
        int i = payService.delete(id);
        return ReturnResponse.success(String.format("成功删除了%s条记录", i));
    }

    @PutMapping("/provider/pay/update")
    public ReturnResponse<String> update(@RequestBody PayDTO payDTO) {
        Pay pay = BeanUtil.copyProperties(payDTO, Pay.class);
        int i = payService.updatePay(pay);
        return ReturnResponse.success(String.format("成功修改了%s条记录", i));
    }

    @PostMapping("/provider/pay/add")
    public ReturnResponse<String> add(@RequestBody PayDTO payDTO) {
        Pay pay = BeanUtil.copyProperties(payDTO, Pay.class);
        int i = payService.addPay(pay);
        return ReturnResponse.success(String.format("成功添加了%s条记录", i));
    }


}
